﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 内容模型【不包含字段】  添加 修改
//最后修改: 2010-11-24
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Web.Admin
{
    public partial class Model_list : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_ContentModel;
        }
        #endregion


        #region 页面变量
        protected string m_strTitle = "内容模型管理";
        ModelManage m_ModelManage = new ModelManage();
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
            }
        }
        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            int intModelId = 0;
            int.TryParse(lbtn.CommandArgument, out intModelId);
            /*
            说明： 删除模型时应判断模型表中是否有数据，如果有则不能删除模型。
             */
            base.SaveLog(Plugin.LogKeys.OperObject.ModelOper, Plugin.LogKeys.OperResult.DelSubmit, intModelId.ToString());
            SubmitAlertDelete(m_ModelManage.DeleteInfoById(intModelId));//删除模型，并且删除表
            LoadBase();
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            base.AlertForNoPermissionControl(TRPOP.CMS.Plugin.VersionLimit.ContentModelAdd);
            string strMSG = "模型名 、表名";
            if (!PageAssist.CheckForm(txtName, ref strMSG) ||
                !PageAssist.CheckForm(txtTableName, ref strMSG)
                )
            {
                Alert(strMSG);
                return;
            }
            if (txtTableName.TextTrim.IndexOf(SystemInfoHelper.getSystemInfo().DBprefixTableName + "_content_") != 0)
            {
                Alert("数据表必须以" + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_content_ 作为前缀");
                return;
            }
            if (txtTableName.TextTrim.Length == (SystemInfoHelper.getSystemInfo().DBprefixTableName + "_content_").Length)
            {
                Alert("数据表命名规则不对");
                return;
            }
            List<ModelInfo> list = m_ModelManage.GetListByCondition(0, "and TableName='" + txtTableName.TextFilterSQL + "'", "");
            if (list == null || list.Count > 0)
            {
                Alert("您指定的数据表在数据库中已存在!");
                return;
            }
            ModelInfo oModelInfo = new ModelInfo();
            oModelInfo.AddTime = DateTime.Now;
            oModelInfo.AddUserId = base.mUserBaseInfo.UserBaseId;
            //oModelInfo.Description = Utils.RemoveHtml(txtDescription.TextFilterSQL);
            oModelInfo.Name = txtName.TextFilterSQL.RemoveHtml();
            oModelInfo.TableName = txtTableName.TextFilterSQL.RemoveHtml();
            oModelInfo.UpdateTime = DateTime.Now;
            oModelInfo.UpdateUserId = base.mUserBaseInfo.UserBaseId;
            EnumSubmitResult enumResult = m_ModelManage.Save(oModelInfo);
            base.SaveLog(Plugin.LogKeys.OperObject.ModelOper, Plugin.LogKeys.OperResult.AddSubmit, oModelInfo.ModelId.ToString());
            SubmitAlert(enumResult);
            //txtDescription.Text = "";
            txtName.Text = "";
            LoadBase();
        }
        #endregion

        #region 页面方法
        public void LoadBase()
        {
            txtTableName.Text = SystemInfoHelper.getSystemInfo().DBprefixTableName + "_content_" + (m_ModelManage.GetMaxId() + 1).ToString();
            rptList.DataSource = m_ModelManage.GetListByCondition(0, "", " ModelId asc");
            rptList.DataBind();
        }
        #endregion
    }
}
